package com.jd.service.impl;

import com.jd.common.Page;
import com.jd.dao.AdminMapper;
import com.jd.pojo.Admin;
import com.jd.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    /**
     * 管理员列表
     * @param page
     * @param rows
     * @param adminname
     * @return
     */
    @Override
    public Page<Admin> selectAdminList(Integer page, Integer rows, String adminname) {

        Admin admin = new Admin();
        if (StringUtils.isNotBlank(adminname)) {
            admin.setAdminname(adminname);
        }

        // 当前页的第一个
        admin.setStart((page-1)*rows);
        // 每页条数
        admin.setRows(rows);

        //查询列表
        List<Admin> admins  = adminMapper.selectAdminList(admin);
        //总记录数
        Integer count = adminMapper.selectAdminCount(admin);
        Page<Admin> result = new Page<>();
        result.setPage(page);
        result.setRows(admins);
        result.setSize(rows);
        result.setTotal(count);
        return result;
    }


    /**
     * 添加管理员
     * @param admin
     */
    @Override
    public void addAdmin(Admin admin) {

        admin.setRole(1);
        int insert = adminMapper.insert(admin);
        System.out.println("添加成功？ " + insert);
    }

    @Override
    public void updateAdmin(Admin admin) {
        int i = adminMapper.updateByPrimaryKey(admin);
        System.out.println("修改成功？ "+ i);
    }

    /**
     * 回显管理员信息
     * @param id
     * @return
     */
    @Override
    public Admin getAdminById(Integer id) {

        Admin admin = adminMapper.selectByPrimaryKey(id);
        return admin;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteAdmin(Integer id) {
        int i = adminMapper.deleteByPrimaryKey(id);
        System.out.println("删除成功？ " +i);
    }


}
